##################################################################
#### Master File for Income, Education, and Policy Priorities ####
##################################################################

print("Starting replication...")

#### Load required libraries ####
install.packages("librarian")
librarian::shelf(tidyverse, lfe, here, rio, patchwork, arrow, kableExtra, lfe)

#### Pathing ####
# Create an R Project in your main directory
# Confirm code, data, figures, and tables are subfolders of the main directory
# here() will default to the project folder as the head directory

#### Load helper functions #####
source(here("code","conjoint_functions.R"))

#### Load Policy Labels ####
policies    = import(here("data","policies.rds"))
policy_list = import(here("data","policy_list.rds"))

#### Load Data ####
dta_grp = read_parquet(here("data","dta_final.parquet"))

#### Generate Plot Data ####
# Takes time (several hours), only run for purpose of replication.
# Resulting data are in the "data" subdirectory
source(here("code","gen_plotdata.R"), print.eval = T)

sink(here("replication_log.txt"), type = "output")

#### Descriptives ####
source(here("code","gen_descriptives.R"), print.eval = T)

#### Figures ####

# Figure 2: Unconditional AMCE
source(here("code", "gen_fig_conjoint-unconditional.R"), print.eval = T)

# Figure 3: Conditional AMCE (Income/Education)
source(here("code", "gen_fig_inc-edu.R"), print.eval = T)

# Figure 4: Conditional AMCE w/ Intensity (Income, Points)
source(here("code", "gen_fig_inc-agree-points.R"), print.eval = T)

# Figure 5: Conditional AMCE w/ Intensity (Income, Scatterplot)
source(here("code", "gen_fig_inc-agree-scatter.R"), print.eval = T)

# Figure 6: Conditional AMCE w/ Intensity (Education)
source(here("code", "gen_fig_edu-agree-points.R"), print.eval = T)

# Figure 7: Conditional AMCE w/ Intensity (Education, Scatterplot)
source(here("code", "gen_fig_edu-agree-scatter.R"), print.eval = T)

# Figure 8: Conditional AMCE w/ Intensity (Republican x Education)
source(here("code", "gen_fig_edu-republican-scatter.R"), print.eval = T)

#### Tables ####

# Table 1: Correlations between high/low income/education revealed preferences
# Also generates Table S9 (high/low education Republican correlations)
source(here("code","gen_tab_cor.R"), print.eval = T)

# Table 2: Conditional AMCE Differences for Economic Issues
source(here("code", "gen_tab_econ.R"), print.eval = T)

#### Appendix ####

# Table S2-7: Appendix B Full Results
source(here("code","gen_appendix_tabs.R"), print.eval = T)

# Table S8: Deming Slopes: high/low income/education revealed importance by policy class
source(here("code", "gen_tab_deming.R"), print.eval = T)

# Table S9: Correlations for high/low education Republican revealed importance by policy class
# See Table 1 Code

# Tables S10-12: AMCE Difference in means by policy type
source(here("code","gen_agr_mods.R"), print.eval = T)

# Figure S1: Alternative income threshold (unconditional AMCE)
source(here("code", "gen_fig_s1.R"), print.eval = T)

# Figure S2: Alternative income threshold (agree/disagree)
source(here("code", "gen_fig_s2.R"), print.eval = T)

# Figure S3: Alternative income threshold (correlation)
source(here("code", "gen_fig_s3.R"), print.eval = T)

# Figure S4: Conditional AMCE holding income constant
source(here("code", "gen_fig_s4.R"), print.eval = T)

# Figure S5: Conditional AMCE correlation holding income constant
source(here("code", "gen_fig_s5.R"), print.eval = T)

# Figures S6-8: Alternative Demographics
source(here("code", "gen_fig_altdemos.R"), print.eval = T)

# Figure S9: Distribution of CAMCE
source(here("code","gen_fig_range.R"), print.eval = T)

# Figure S10: Differences in high/low education support for policies
source(here("code","gen_fig_svyagree.R"), print.eval = T)

sink()

print("Replication Completed. See output in replication_log.txt")
